
let layer = layui.layer;
let form = layui.form;

// 获取友情链接
function getList() {
    $.ajax({
        url: '/admin/links',
        success: function (res) {
            let str = '';
            if (res.data) {
                res.data.forEach(item => {
                    str += `
                    <tr>
                        <td>${item.id}</td>
                        <td><img src="http://localhost:8888/uploads/${item.linkicon}" alt=""></td>
                        <td>${item.linkname}</td>
                        <td>${item.linkurl}</td>
                        <td>${item.linkdesc}</td>
                        <td>
                            <button class="layui-btn layui-btn-sm layui-btn-danger edit" data-id="${item.id}">编辑</button>
                            <button class="layui-btn layui-btn-sm" id="del">删除</button>
                        </td>
                    </tr>
                    `;
                });
                $('tbody').html(str);
            }

        }
    });
}

getList();

// 添加数据
$('#add-link').click(function () {

    let index = layer.open({
        type: 1,
        title: '添加友情链接',
        content: $('#add-form-tpl').html(),
        area: ['700px', '500px']
    });
    $('#urlIcon').click(function () {
        $('#linkFile').click();
    });

    $('#linkFile').change(function () {
        let obj = URL.createObjectURL(this.files[0]);
        $('#preIcon').attr('src', obj);
    })

    $('#add-form').submit(function (e) {
        e.preventDefault();
        let fd = new FormData(this);
        $.ajax({
            type: 'post',
            url: '/admin/links ',
            data: fd,
            processData: false,
            contentType: false,
            success: function (res) {
                if (res.status === 0) {
                    layer.close(index);
                    layer.msg(res.message);
                    getList();
                }
            }
        });
    })

});

// 删除数据
$('.layui-table tbody').on('click', '#del', function () {

    let id = $(this).parent().attr('data-id');
    console.log(id);

    layer.confirm('确实要删除吗？', function (index) {
        $.ajax({
            type: 'delete',
            url: '/admin/links/' + id,
            success: function (res) {
                console.log(res);
                if (res.status === 0) {
                    layer.close(index);
                    getList();
                }
            }
        });
    });

});
// 编辑功能
$('.layui-table tbody').on('click', '.edit', function () {
    let id = $(this).attr('data-id');
    $.ajax({
        url: `/admin/links/${id}`,
        type: 'get',
        success: function (res) {
            let index = layer.open({
                type: 1,
                title: '编辑友情链接',
                content: $('#edit-form-tpl').html(),
                area: ['500px', '350px']
            });
            $('#preIcon').attr('src', 'http://localhost:8888/uploads/' + res.data.linkicon);
            form.val('editForm', res.data);

            $('#urlIcon').click(function () {
                $('#linkFile').click();
            });
            let file = null;
            $('#linkFile').change(function () {
                let file = $('.linkFile')[0].files[0];
                let obj = URL.createObjectURL(file);
                console.log(obj);
                $('#preIcon').attr('src', obj);
            });

            $('#edit-form').submit(function (e) {
                e.preventDefault();

                let fd = new FormData(this);
                if (file) {
                    fd.append('linkicon', file);
                }
                $.ajax({
                    type: 'put',
                    url: `/admin/links/${id}`,
                    data: fd,
                    processData: false,
                    contentType: false,
                    success: function (res) {
                        layer.close(index);
                        getList();
                    }
                })
            })
            $('.remove').click(function (e) {
                e.preventDefault()
                form.val('editForm', res.data);
            })
        }
    })
});
